// pages/search/search.js
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keywords: '',
    page: 1,
    goods: [],
    empty: false,
    curRed1: 'sr',
    curRed2: 'sh',
    curRed3: 'sh',
    domain: getApp().globalData.domain,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var keywords = options.keywords
    if (keywords != '') {
      this.setData({ keywords: keywords })
      this.getGoods(keywords, 1) //默认取第1页
    } else {
      this.setData({ empty: true })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  getGoods: function (keywords, page) {
    var that = this;
    wx.request({
      url: getApp().globalData.domain + 'api/commodity/search?keyword=' + keywords,
      data: { keywords: keywords, page: page },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        console.log(res.data.data);
        that.setData({
          goods: res.data.data,
        })
      },
      fail: function (err) { },
      complete: function () { }
    })
  },
  inputSearch: function (e) {
    this.setData({keywords: e.detail.value })
  },
  search: function () {
    // this.setData({ keywords: searchContent })
    var keywords = this.data.keywords
    this.setData({ goods: [], page: 1, curRed1: 'sr', curRed2: 'sh', curRed3: 'sh', })
    // console.log(curRed1)
    if (keywords != '') {
      this.getGoods(keywords, 1)
    }
  },
  /**
   * 搜索功能
   */
  searchGoods: function () {
    var keywords = this.data.keywords
    this.setData({ goods: [], page: 1 })
    if (keywords != '') {
      this.getGoods(keywords, 1)
    }
  },
  saleVolume: function () {
    var keywords = this.data.keywords
    this.setData({ goods: [], page: 1, curRed1: 'sh', curRed2: 'sr', curRed3: 'sh', })
    if (keywords != '') {
      this.getGoodVo(keywords, 1)
    }
  },
  getGoodVo: function (keywords, page) {
    var that = this;
    wx.request({
      url: getApp().globalData.domain + 'api/commodity/sales_volume_desc?keyword=' + keywords,
      data: { keywords: keywords, page: page },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        console.log(res.data.data);
        that.setData({
          goods: res.data.data,
        })
      },
      fail: function (err) { },
      complete: function () { }
    })
  },
  salePrice: function () {
    var keywords = this.data.keywords
    this.setData({ goods: [], page: 1, curRed1: 'sh', curRed2: 'sh', curRed3: 'sr', })
    if (keywords != '') {
      this.getGoodPr(keywords, 1)
    }
  },
  getGoodPr: function (keywords, page) {
    var that = this;
    wx.request({
      url: getApp().globalData.domain + 'api/commodity/price_desc?keyword=' + keywords,
      data: { keywords: keywords, page: page },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        console.log(res.data.data);
        that.setData({
          goods: res.data.data,
        })
      },
      fail: function (err) { },
      complete: function () { }
    })
  },
})